//
//  HWMineController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/26.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit

class HWMineController: HWBaseViewController {
    var topBgView:UIView!
    var collectionView:UICollectionView!
    let collectionProtocol = HWMineProtocol()
    let httpManager = HWHttpManager()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        
        initFrame()
        
        addAction()
        
        loadData()
        
        addRefresh()
        
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
    }
}

extension HWMineController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        navigationView.backButton.isHidden = true
        navigationView.titleLabel.isHidden = true
        navigationView.backgroundColor = .colorWithHexString(hexString: "#5879FF")
        
        view.backgroundColor = .colorWithHexString(hexString: "#F5F5F5")
        
        topBgView = UIView()
        topBgView.backgroundColor = .colorWithHexString(hexString: "#5879FF")
        view.addSubview(topBgView)
        
        let flowL = JJCollectionViewRoundFlowLayout_Swift.init()
        flowL.minimumLineSpacing = 0
        flowL.minimumInteritemSpacing = 0
        flowL.scrollDirection = .vertical
        flowL.isCalculateHeader = true
        flowL.isCalculateFooter = false
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.backgroundColor = .clear
        collectionView.register(HWMineTopCell.self, forCellWithReuseIdentifier: "HWMineTopCell")
        collectionView.register(HWMineNoticeCell.self, forCellWithReuseIdentifier: "HWMineNoticeCell")
        collectionView.register(HWMineFuncCell.self, forCellWithReuseIdentifier: "HWMineFuncCell")
        collectionView.register(HWMineLogOutCell.self, forCellWithReuseIdentifier: "HWMineLogOutCell")
        collectionView.register(HWMineHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HWMineHeaderView")
        view.addSubview(collectionView)
        
    }
    
    private func initFrame() -> Void {
        topBgView.snp.makeConstraints { make in
            make.left.right.top.equalTo(0)
            make.height.equalTo(232*widthScale)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(74*widthScale)
        }
    }
}

extension HWMineController {
    func addAction() -> Void {
        collectionProtocol.didClickItem = {
            model in
            switch HWMineNavType(rawValue: model.type) {
            case .none: break
            /// 推广盒子
            case .some(.promoteBox):
                let qrCode = HWPromotionQrCodeViewController()
                self.navigationController?.pushViewController(qrCode, animated: true)
            /// 推广游戏
            case .some(.promoteGame):break
            /// 我的游戏
            case .some(.myGame):
//                let promoteList = HWPromoteListController()
//                promoteList.hideNav = false
//                self.navigationController?.pushViewController(promoteList, animated: true)
                let searchGame = HWPromoteSearchController()
                self.navigationController?.pushViewController(searchGame, animated: true)
            /// 申请游戏
            case .some(.applyGame):
                self.navigationController?.pushViewController(HWApplyGameViewController(), animated: true)
            /// 折扣代充
            case .some(.saleRecharge):
                self.navigationController?.pushViewController(HWDiscountDaiChunViewController(), animated: true)
            /// 申请结算
            case .some(.applySettlement):
                self.navigationController?.pushViewController(HWApplyjiesuanViewController(), animated: true)
            /// 结算明细
            case .some(.settlementDetailed):
                self.navigationController?.pushViewController(HWBillingDetailsViewController(), animated: true)
            /// 分成明细
            case .some(.divisionDetails):
                self.navigationController?.pushViewController(HWDividedIntoViewController(), animated: true)
            /// 下线管理
            case .some(.subordinate):
                self.navigationController?.pushViewController(MyOfflineViewController(), animated: true)
            /// 提现管理
            case .some(.withdrawalManage):
                self.navigationController?.pushViewController(HWTixianguanliViewController(), animated: true)
            /// 添加下线
            case .some(.addSubordinate):
                self.navigationController?.pushViewController(HWtianjiangxianxiangController(), animated: true)
            /// 分成比例
            case .some(.divideScale):
                self.navigationController?.pushViewController(HWDividedintoProportionViewController(), animated: true)
            /// 开服信息
            case .some(.openServiceInfo):
                self.navigationController?.pushViewController(HWOpenServiceInfoController(), animated: true)
            /// 游戏礼包
            case .some(.gameGiftBag):
                self.navigationController?.pushViewController(HWGameGiftBagController(), animated: true)
            /// 注册统计
            case .some(.registerStatistics):
                self.navigationController?.pushViewController(HWRegistrationStatisticsViewController(), animated: true)
            /// 充值统计
            case .some(.rechargeStatistics):
                self.navigationController?.pushViewController(HWTopupStatisticsViewController(), animated: true)
            /// 角色汇总
            case .some(.roleSummary):
                self.navigationController?.pushViewController(HWRoleSummaryViewController(), animated: true)
            /// 注册方式
            case .some(.registType):
                let typeController = HWRegistTypeController()
                typeController.type = .registType
                self.navigationController?.pushViewController(typeController, animated: true)
            /// 充值方式
            case .some(.rechargeType):
                let typeController = HWRegistTypeController()
                typeController.type = .rechargeType
                self.navigationController?.pushViewController(typeController, animated: true)
            /// 消费榜单
            case .some(.consumeRank):
                let rankList = HWRankListController()
                rankList.type = .consumeRank
                self.navigationController?.pushViewController(rankList, animated: true)
            /// 推广排行
            case .some(.promoteRank):
                self.navigationController?.pushViewController(HWtuiguanpanhangbangViewController(), animated: true)
            /// 个人信息
            case .some(.mineInfo):
                self.navigationController?.pushViewController(HWPersonalInformationViewController(), animated: true)
            /// 实名认证
            case .some(.realNameAuthentication):
                let my = MyViewController()
                my.navController = self.navigationController!
                my.shifoshimingrenzheng()
            /// 修改密码
            case .some(.changePassword):
                self.navigationController?.pushViewController(HWChangeThePasswordViewController(), animated: true)
            /// 充值记录
            case .some(.rechargeRecord):
                self.navigationController?.pushViewController(HWPrepaidPhoneRecordsjiluViewController(), animated: true)
            /// 提现明细
            case .some(.withdrawalDetails):break
            /// 联系我们
            case .some(.contactUs):
                self.navigationController?.pushViewController(HWContactUsViewController(), animated: true)
            /// 人气榜
            case .some(.popularityRank):
                let rankList = HWRankListController()
                rankList.type = .popularityRank
                self.navigationController?.pushViewController(rankList, animated: true)
            /// 推广公告
            case .some(.promoteNotice):
                let promoteNotice = HWPromotionAnnouncementViewController()
                self.navigationController?.pushViewController(promoteNotice, animated: true)
            }
        }
        
        collectionProtocol.withdrawalButtonClick = {
            mod in
            let apply = HWApplyjiesuanViewController()
            apply.jisuanStr = mod.shareMoney
            self.navigationController?.pushViewController(apply, animated: true)
        }
        
        collectionProtocol.rechargeButtonClick = {
            mod in
            let chongzhi = HWChangchyongViewController()
            chongzhi.cunchuStr = mod.yufu
            self.navigationController?.pushViewController(chongzhi, animated: true)
        }
        
        collectionProtocol.didClickNotice = {
            mod in
            if mod.url.count > 0 {
                let webView = HWWebViewController()
                webView.urlString = mod.url
                self.navigationController?.pushViewController(webView, animated: true)
            }
        }
        
        collectionProtocol.logOut = {
//            NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
//            [defaults  removeObjectForKey:@"uid"];
//            [defaults  removeObjectForKey:@"username"];
//             [defaults  removeObjectForKey:@"head"];
//            [defaults synchronize];
//            HWLoginViewController *login = [[HWLoginViewController alloc]init];
//           kKeyWindow.rootViewController = login;
            UserDefaults.standard.removeObject(forKey: "uid")
            UserDefaults.standard.removeObject(forKey: "username")
            UserDefaults.standard.removeObject(forKey: "head")
            let login = HWLoginViewController()
            currentWindow?.rootViewController = login
        }
    }
}

extension HWMineController {
    func loadData() -> Void {
        httpManager.getMineInfo { infoModel in
            self.collectionView.mj_header?.endRefreshing()
            self.collectionProtocol.model = infoModel
            self.collectionView.reloadData()
        } failure: { error in
            self.collectionView.mj_header?.endRefreshing()
            self.view.showHUD(title: error)
        }
    }
    
    func addRefresh() -> Void {
        collectionView.mj_header = MJRefreshNormalHeader()
        collectionView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
    }
    
    @objc func loadNewData() -> () {
        loadData()
    }
}
